package com.google.code.twithis.foursix.ui.component;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;

public class ImageButton extends BitmapField {

	private String text;

	private int padding, color, focusColor, fontColor, focusFontColor;

	public ImageButton() {
		super(null, Field.FOCUSABLE);
	}

	public ImageButton(int padding) {
		this();
		this.padding = padding;
	}

	public ImageButton(String text, int padding, int color, int focusColor, int fontColor, int focusFontColor) {
		this();
		this.text = text;
		this.padding = padding;
		this.color = color;
		this.focusColor = focusColor;
		this.fontColor = fontColor;
		this.focusFontColor = focusFontColor;
	}

	protected boolean navigationClick(int status, int time) {
		if (getChangeListener() != null) {
			getChangeListener().fieldChanged(this, status);
		}

		return true;
	}

	protected void onDisplay() {
		Font font = this.getFont();
		int width = font.getAdvance(text);
		int height = font.getHeight();

		setExtent(width + (padding * 2), height + (padding * 2));
		// super.onDisplay();
	}

	protected void paint(Graphics g) {
		Font font = this.getFont();
		int width = font.getAdvance(text);
		int height = font.getHeight();

		g.setColor(color);
		g.fillRoundRect(0, 0, width + (padding * 2), height + (padding * 2), 15, 15);

		g.setColor(fontColor);
		g.drawText(text, padding, padding);

		// super.paint(graphics);
	}

	protected void drawFocus(Graphics g, boolean on) {
		if (on) {
			Font font = this.getFont();
			int width = font.getAdvance(text);
			int height = font.getHeight();

			g.setColor(focusColor);
			g.fillRoundRect(0, 0, width + (padding * 2), height + (padding * 2), 15, 15);

			g.setColor(focusFontColor);
			g.drawText(text, padding, padding);
		}

		// super.drawFocus(graphics, on);
	}

}
